package com.pc;


import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程之间的通信问题：生产者和消费者问题  等待唤醒，通知唤醒
 * 线程交替执行  A  B  操作同一个变量  num = 0
 *
 * @author by lvshh
 * @version 1.0
 * @date 2020/5/8 12:14
 */

public class A {


    public static void main(String[] args) {
        Data data = new Data();

        new  Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new  Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new  Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new  Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printD();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}

class Data{//数字，资源类
    private int num = 1;

    Lock lock = new  ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    Condition condition4 = lock.newCondition();


    //+1
    public  void printA() throws InterruptedException {
        lock.lock();
        try {
            while (num != 1){
                condition1.await();
            }
            num = 2;
            //通知其他线程
            System.out.println(Thread.currentThread().getName() + "AAAAA");
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    //-1
    public  void printB() throws InterruptedException {
        lock.lock();
        try {

            while (num != 2){
                condition2.await();
            }
            num = 3;
            condition3.signal();
            System.out.println(Thread.currentThread().getName() + "BBBBB" );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public  void printC() throws InterruptedException {
        lock.lock();
        try {

            while (num != 3){
                condition3.await();
            }
            num = 4;
            condition4.signal();
            System.out.println(Thread.currentThread().getName() + "CCCCC");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public  void printD() throws InterruptedException {
        lock.lock();
        try {

            while (num != 4){
                condition4.await();
            }
            num = 1;
            condition1.signal();
            System.out.println(Thread.currentThread().getName() + "DDDDDDD");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }



}
